﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using STLGROUP.Layers.Presentations.FormBase;
using STLGROUP.Layers.Common;
using STLGROUP.Layers.DataAccessLayers;

namespace QuanLyBanHang
{
    /// <summary>
    /// PF004  - thanh toan don hang
    /// Mode 1: DonHang
    /// Mode 2: HoaDon
    /// </summary>
    /// <author>viet sang</author>
    public partial class PF004 : frmBase
    {
        private int _idDonHang;
        private int _mode;        

        public int Mode
        {
            get { return _mode; }
            set { _mode = value; }
        }

        public int IdDonHang
        {
            get { return _idDonHang; }
            set { _idDonHang = value; }
        }

        public PF004()
        {
            InitializeComponent();
            txtThanhToan.Focus();

            this.Load += new EventHandler(PF004_Load);
            this.txtThanhToan.Leave += new EventHandler(txtThanhToan_Leave);
        }

        private void PF004_Load(object sender, EventArgs e)
        {
            if (Mode == 1)//don hang
            {
                this.Text = "Thanh toán công nợ nhà cung cấp";
                lblMaDoiTac.Text = "Mã nhà cung cấp:";
                lblTenDoiTac.Text = "Tên nhà cung cấp";

                var data = (from hd in Cache.Context.DT_DonDatHang
                            join cthd in Cache.Context.DT_ChiTietDonHang on hd.IDDonDatHang equals cthd.IDDonDatHang
                            join kh in Cache.Context.DT_NhaCungCap on hd.IDKhachHang equals kh.IDNhaCungCap
                            join nv in Cache.Context.DT_NhanVien on hd.IDNhanVienXacNhan equals nv.IDNhanVien
                            where hd.IDDonDatHang==IdDonHang
                            select new
                            {
                                hd.IDDonDatHang,
                                hd.IDLoaiGiaoDich,
                                hd.MaDonDatHang,
                                hd.TongTien,
                                hd.DaThanhToan,
                                ConLai = hd.TongTien - hd.DaThanhToan,
                                nv.MaNhanVien,
                                HoTenNhanVien = nv.HoVaTenDem + " " + nv.TenNhanVien,
                                kh.MaNhaCungCap,
                                kh.TenNhaCungCap,
                                hd.NgayTao,
                                hd.NgayThanhToan
                            }).Distinct().FirstOrDefault();

                lblMaDonHang.Text = data.MaDonDatHang;
                txtMaDoiTac.Text = data.MaNhaCungCap;
                txtTenDoiTac.Text = data.TenNhaCungCap;
                txtHanThanhToan.Text = data.NgayThanhToan.HasValue ? data.NgayThanhToan.Value.ToString("dd/MM/yyyy") : string.Empty; ;
                txtTongCong.Text =  data.TongTien.HasValue? data.TongTien.Value.ToString(Const.DINH_DANG_HIEN_THI_TIEN):"0";
                txtDaThanhToan.Text = data.DaThanhToan.HasValue ? data.DaThanhToan.Value.ToString(Const.DINH_DANG_HIEN_THI_TIEN) : "0";
                txtConLai.Text = data.ConLai.HasValue ? data.ConLai.Value.ToString(Const.DINH_DANG_HIEN_THI_TIEN) : "0";

            }
            if (Mode == 2)//hoa don
            {
                this.Text ="Thanh toán công nợ khách hàng";
                lblMaDoiTac.Text = "Mã khách hàng:";
                lblTenDoiTac.Text = "Tên Khách hàng";

                var data = (from hd in Cache.Context.DT_HoaDon
                            join cthd in Cache.Context.DT_ChiTietHoaDon on hd.IDHoaDon equals cthd.IDHoaDon
                            join kh in Cache.Context.DT_KhachHang on hd.IDDoiTac equals kh.IDKhachHang into leftJoin
                            from a in leftJoin.DefaultIfEmpty()
                            join nv in Cache.Context.DT_NhanVien on hd.IDNhanVien equals nv.IDNhanVien
                            where hd.IDHoaDon==_idDonHang
                            select new
                            {
                                hd.IDHoaDon,
                                hd.IDLoaiHoaDon,
                                hd.MaHoaDon,
                                hd.TongTien,
                                hd.DaTra,
                                ConLai = hd.TongTien - hd.DaTra,
                                nv.MaNhanVien,
                                HoTenNhanVien = nv.HoVaTenDem + " " + nv.TenNhanVien,
                                a.MaKhachHang,
                                HoTenKhachHang = a.HoDem + " " + a.Ten,
                                hd.NgayTao
                            }).Distinct().FirstOrDefault();

                lblMaDonHang.Text = data.MaHoaDon;
                txtMaDoiTac.Text = data.MaKhachHang;
                txtTenDoiTac.Text = data.HoTenKhachHang;
                txtHanThanhToan.Text = string.Empty;
                txtTongCong.Text = data.TongTien.HasValue ? data.TongTien.Value.ToString(Const.DINH_DANG_HIEN_THI_TIEN) : "0";
                txtDaThanhToan.Text = data.DaTra.HasValue ? data.DaTra.Value.ToString(Const.DINH_DANG_HIEN_THI_TIEN) : "0";
                txtConLai.Text = data.ConLai.HasValue ? data.ConLai.Value.ToString(Const.DINH_DANG_HIEN_THI_TIEN) : "0";
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            decimal thanhToan=0;
            
            if (decimal.TryParse(txtThanhToan.Text, out thanhToan))
            {
                if (thanhToan > decimal.Parse(txtConLai.Text))
                {
                    UICommon.ShowMessgeString("Số tiền thanh toán không được lớn hơn số tiền còn nợ", "Thông báo");
                    return;
                }
                else
                {
                    if (Mode == 1)
                    {
                        DT_DonDatHang donHang = Cache.Context.DT_DonDatHang.Where(s => s.IDDonDatHang == _idDonHang).FirstOrDefault();
                        if (donHang != null)
                        {
                            donHang.DaThanhToan += thanhToan;
                        }
                    }
                    if (Mode == 2)
                    {
                        DT_HoaDon hoaDon = Cache.Context.DT_HoaDon.Where(s => s.IDHoaDon == _idDonHang).FirstOrDefault();
                        if (hoaDon != null)
                        {
                            hoaDon.DaTra += thanhToan;
                        }
                    }

                    int kq = Cache.Context.SaveChanges();
                    if (kq > 0)
                    {
                        UICommon.ShowMessegeInfo(Const.MSG_UPDATE_SUCESSFULL);
                        this.Close();
                    }
                    else                    
                    {
                        UICommon.ShowMessegeInfo(Const.MSG_UPDATE_NOT_SUCESSFULL);
                    }
                }
 
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtThanhToan_Leave(object sender, EventArgs e)
        {
            decimal thanhToan = 0;
            if (decimal.TryParse(txtThanhToan.Text, out thanhToan))
            {
                txtThanhToan.Text = thanhToan.ToString(Const.DINH_DANG_HIEN_THI_TIEN);
            }
        }

    }
}
